home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Sound / PreludeAMP / src / getbits.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-25  |  6.5 KB  |  263 lines

  1.  
  2. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  3. */
  4.  
  5. /* getbits.c  bit level routines, input buffer
  6.  * 
  7.  * Created by: tomislav uzelac  Apr 1996 
  8.  * better synchronization, tomislav uzelac, Apr 23 1997
  9.  */
  10. #include "amiga.h"
  11. #include "amp.h"
  12. #include "audio.h"
  13. #include "formats.h"
  14. #include "rtbuf.h"
  15.  
  16. #define    GETBITS
  17. #include "getbits.h"
  18.  
  19. /* 
  20.  * buffer and bit manipulation functions ***************************************
  21.  */
  22. static inline int _fillbfr(unsigned int size)
  23. {
  24.     _bptr=0;
  25.         return get_input(_buffer, size);
  26. }
  27.  
  28. static inline int readsync()
  29. {
  30.     _bptr=0;
  31.     _buffer[0]=_buffer[1];
  32.     _buffer[1]=_buffer[2];
  33.     _buffer[2]=_buffer[3];
  34.     return get_input(&_buffer[3],1);
  35. }
  36.  
  37. static inline unsigned int _getbits(int n)
  38. {
  39. unsigned int pos,ret_value;
  40.  
  41.         pos = _bptr >> 3;
  42.     ret_value = _buffer[pos] << 24 |
  43.             _buffer[pos+1] << 16 |
  44.             _buffer[pos+2] << 8 |
  45.             _buffer[pos+3];
  46.         ret_value <<= _bptr & 7;
  47.         ret_value >>= 32 - n;
  48.         _bptr += n;
  49.         return ret_value;
  50. }       
  51.  
  52. int fillbfr(unsigned int advance)
  53. {
  54. int overflow,retval;
  55.  
  56.         retval=get_input(&buffer[append], advance);
  57.     
  58.     if ( append + advance >= BUFFER_SIZE ) {
  59.         overflow = append + advance - BUFFER_SIZE;
  60.         memcpy (buffer,&buffer[BUFFER_SIZE], overflow);
  61.         if (overflow < 4) memcpy(&buffer[BUFFER_SIZE],buffer,4);
  62.         append = overflow;
  63.     } else {
  64.         if (append==0) memcpy(&buffer[BUFFER_SIZE],buffer,4);
  65.         append+=advance;
  66.     }
  67.     return retval;
  68. }
  69.  
  70. unsigned int getbits(int n)
  71. {
  72.         if (n) {
  73.         unsigned int pos,ret_value;
  74.  
  75.                 pos = data >> 3;
  76.                 ret_value = buffer[pos] << 24 |
  77.                         buffer[pos+1] << 16 |
  78.                         buffer[pos+2] << 8 |
  79.                         buffer[pos+3];
  80.                 ret_value <<= data & 7;
  81.                 ret_value >>= 32 - n;
  82.  
  83.                 data += n;
  84.                 data &= (8*BUFFER_SIZE)-1;
  85.  
  86.                 return ret_value;
  87.         } else
  88.                 return 0;
  89. }
  90.  
  91. /*
  92.  * header and side info parsing stuff ******************************************
  93.  */
  94. static inline void parse_header(struct AUDIO_HEADER *header) 
  95. {
  96.         header->ID=_getbits(1);
  97.         header->layer=_getbits(2);
  98.         header->protection_bit=_getbits(1);
  99.         header->bitrate_index=_getbits(4);
  100.         header->sampling_frequency=_getbits(2);
  101.         header->padding_bit=_getbits(1);
  102.         header->private_bit=_getbits(1);
  103.         header->mode=_getbits(2);
  104.         header->mode_extension=_getbits(2);
  105.         if (!header->mode) header->mode_extension=0;
  106.         header->copyright=_getbits(1);
  107.         header->original=_getbits(1);
  108.         header->emphasis=_getbits(2);
  109. }
  110.  
  111. static inline int header_sanity_check(struct AUDIO_HEADER *header)
  112. {
  113.     if (     header->layer==0 ||
  114.         header->bitrate_index==15 ||
  115.         header->sampling_frequency==3) return -1;
  116.  
  117.     /* an additional check to make shure that stuffing never gets mistaken
  118.       * for a syncword. This rules out some legal layer1 streams, but who
  119.       * cares about layer1 anyway :-). I must get this right sometime.
  120.      */
  121.     if ( header->ID==1 && header->layer==3 && header->protection_bit==1) return -1;
  122.     return 0;
  123. }
  124.  
  125.  
  126. int gethdr(struct AUDIO_HEADER *header)
  127. {
  128. int s,retval;
  129. struct AUDIO_HEADER tmp;
  130.  
  131.     /* TODO: add a simple byte counter to check only first, say, 1024
  132.      * bytes for a new header and then return GETHDR_SYN
  133.      */
  134.     if ((retval=_fillbfr(4))!=0) return retval;
  135.  
  136.     for(;;) {
  137.         while ((s=_getbits(12)) != 0xfff) {
  138.             if (s==0xffe) {
  139.                 parse_header(&tmp);
  140.                 if (header_sanity_check(&tmp)==0) return GETHDR_NS;
  141.             }
  142.             if ((retval=readsync())!=0) return retval;
  143.         }
  144.     
  145.         parse_header(&tmp);
  146.         if (header_sanity_check(&tmp)!=0) {
  147.             if ((retval=readsync())!=0) return retval;
  148.         } else break;
  149.     }
  150.  
  151.     if (tmp.layer==3) return GETHDR_FL1;
  152.     /* if (tmp.layer==2) return GETHDR_FL2; */
  153.     if (tmp.bitrate_index==0) return GETHDR_FF;
  154.  
  155.     memcpy(header,&tmp,sizeof(tmp));
  156.     return 0;
  157. }
  158.  
  159. /* dummy function, to get crc out of the way
  160. */
  161. void getcrc()
  162. {
  163.     _fillbfr(2);
  164.     _getbits(16);
  165. }
  166.  
  167. /* sizes of side_info:
  168.  * MPEG1   1ch 17    2ch 32
  169.  * MPEG2   1ch  9    2ch 17
  170.  */
  171. void getinfo(struct AUDIO_HEADER *header,struct SIDE_INFO *info)
  172. {
  173. int gr,ch,scfsi_band,region,window;
  174. int nch;    
  175.     if (header->mode==3) {
  176.         nch=1;
  177.         if (header->ID) {
  178.             _fillbfr(17);
  179.             info->main_data_begin=_getbits(9);
  180.             _getbits(5);
  181.         } else {
  182.             _fillbfr(9);
  183.             info->main_data_begin=_getbits(8);
  184.             _getbits(1);
  185.         }
  186.     } else {
  187.         nch=2;
  188.                 if (header->ID) {
  189.             _fillbfr(32);
  190.                         info->main_data_begin=_getbits(9);
  191.                         _getbits(3);
  192.                 } else {
  193.             _fillbfr(17);
  194.                         info->main_data_begin=_getbits(8);
  195.                         _getbits(2);
  196.                 }
  197.     }
  198.  
  199.     if (header->ID) for (ch=0;ch<nch;ch++)
  200.         for (scfsi_band=0;scfsi_band<4;scfsi_band++)
  201.             info->scfsi[ch][scfsi_band]=_getbits(1);
  202.  
  203.     for (gr=0;gr<(header->ID ? 2:1);gr++)
  204.         for (ch=0;ch<nch;ch++) {
  205.             info->part2_3_length[gr][ch]=_getbits(12);
  206.             info->big_values[gr][ch]=_getbits(9);
  207.             info->global_gain[gr][ch]=_getbits(8);
  208.             if (header->ID) info->scalefac_compress[gr][ch]=_getbits(4);
  209.             else info->scalefac_compress[gr][ch]=_getbits(9);
  210.             info->window_switching_flag[gr][ch]=_getbits(1);
  211.  
  212.             if (info->window_switching_flag[gr][ch]) {
  213.                 info->block_type[gr][ch]=_getbits(2);
  214.                 info->mixed_block_flag[gr][ch]=_getbits(1);
  215.  
  216.                 for (region=0;region<2;region++)
  217.                     info->table_select[gr][ch][region]=_getbits(5);
  218.                 info->table_select[gr][ch][2]=0;
  219.  
  220.                 for (window=0;window<3;window++)
  221.                     info->subblock_gain[gr][ch][window]=_getbits(3);
  222.             } else {
  223.                 for (region=0;region<3;region++)
  224.                     info->table_select[gr][ch][region]=_getbits(5);
  225.  
  226.                 info->region0_count[gr][ch]=_getbits(4);
  227.                 info->region1_count[gr][ch]=_getbits(3);
  228.                 info->block_type[gr][ch]=0;
  229.             }
  230.  
  231.             if (header->ID) info->preflag[gr][ch]=_getbits(1);
  232.             info->scalefac_scale[gr][ch]=_getbits(1);
  233.             info->count1table_select[gr][ch]=_getbits(1);
  234.         }
  235.     return;
  236. }
  237.  
  238. int dummy_getinfo(int n)
  239. {
  240.     n-=4;
  241.         if ( fseek(in_file,n,SEEK_CUR) != 0)
  242.                 if (feof(in_file)) return GETHDR_EOF;
  243.                 else return GETHDR_ERR;
  244.     return 0;
  245. }
  246.  
  247. int rewind_stream(int nbytes) {
  248.     nbytes+=5;
  249.     if (fseek(in_file, -nbytes, SEEK_CUR) != 0) {
  250.         /* what if we need to be at the very beginning? */
  251.         nbytes--;
  252.         if (fseek(in_file, -nbytes, SEEK_CUR) != 0) return GETHDR_ERR;
  253.     }
  254.     return 0;
  255. }
  256.  
  257. static inline int get_input(unsigned char* bp, unsigned int size) {
  258.     if(fread(bp, 1, size, in_file) != size) 
  259.         if (feof(in_file)) return GETHDR_EOF;
  260.         else               return GETHDR_ERR;
  261.     return 0;
  262. }
  263.